#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
自动清空数据库中的所有历史数据
"""
import sqlite3
import os

def clear_all_data():
    """清空所有数据表的数据"""
    
    if not os.path.exists('performance.db'):
        print("数据库文件不存在")
        return
    
    conn = sqlite3.connect('performance.db')
    cursor = conn.cursor()
    
    try:
        # 获取所有表名
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        
        print("数据库中的表:")
        for table in tables:
            print(f"- {table[0]}")
        
        print("\n开始清空数据...")
        
        # 清空每个表的数据
        for table in tables:
            table_name = table[0]
            if table_name != 'sqlite_sequence':  # 跳过系统表
                cursor.execute(f"DELETE FROM {table_name}")
                print(f"✅ 已清空表: {table_name}")
        
        # 重置自增ID
        cursor.execute("DELETE FROM sqlite_sequence")
        print("✅ 已重置自增ID")
        
        conn.commit()
        print("\n🎉 所有数据已成功清空!")
        
        # 验证清空结果
        print("\n验证清空结果:")
        for table in tables:
            table_name = table[0]
            if table_name != 'sqlite_sequence':
                cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
                count = cursor.fetchone()[0]
                print(f"- {table_name}: {count} 条记录")
        
    except Exception as e:
        print(f"❌ 清空数据时出错: {e}")
        conn.rollback()
    
    finally:
        conn.close()

if __name__ == "__main__":
    print("=== 自动清空数据库历史数据 ===")
    clear_all_data()